package com.example.boot.service.list;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.example.boot.bean.bean1.*;
import com.example.boot.bean.bean1.info.FeedInfo;
import com.example.boot.mapper.mapper1.*;
import com.example.boot.utils.getAgeByBirthday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service


public class ListAllServiceImpl implements ListAllService {
    @Autowired
    User1Mapper userMapper;
    @Autowired
    LearningFeedMapper learningFeedMapper;
    @Autowired
    LearningFeedVideoMapper learningFeedVideoMapper;
    @Autowired
    BasicVideoMapper basicVideoMapper;
    @Autowired
    BasicImageMapper basicImageMapper;
    @Autowired
    LearningFeedTopicMapper learningFeedTopicMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    LearningFeedLabelMapper learningFeedLabelMapper;
    @Autowired
    FeedLabelMapper feedLabelMapper;
    @Autowired
    LearningFeedImageMapper learningFeedImageMapper;
    @Autowired
    LearningCategoryMapper learningCategoryMapper;
    @Autowired
    LearningFeedCategoryMapper learningFeedCategoryMapper;





/*
拿到所有内容的feedId集合 foreach循环，根据循环取当前feedId,新建一个集合用来盛放单个动态信息，
根据feedId取得所需其他内容（Video,Topic,User,Label,image,category）并装填，最后将此集合放进将要返回给前端的集合内，
循环完返回集合
 */

 /*   public List<FeedInfo> listAllFeed(){
        ArrayList<FeedInfo> feeds = new ArrayList<>();
        List<LearningFeed> feedsId = learningFeedMapper.selectList(new QueryWrapper<LearningFeed>().lambda().select(LearningFeed::getId));
        FeedInfo feedInfo = new FeedInfo();
        feedsId.forEach(fid->{

            //添加基本信息

            LearningFeed feed = learningFeedMapper.selectById(fid.getId());
            feedInfo.setId(feed.getId());
            feedInfo.setAuthorID(feed.getAuthorId());
            feedInfo.setLikeCount(feed.getLikeCount());
            feedInfo.setPostTime(feed.getPostTime());
            feedInfo.setReplyCount(feed.getReplyCount());
            feedInfo.setText(feed.getType());

            //添加Video

            LearningFeedVideo video = learningFeedVideoMapper.selectById(fid.getId());
            if (video!=null) {
                BasicVideo basicVideo = basicVideoMapper.selectById(video.getVideoID());
                video.setVideoFileName(basicVideo.getFileName());
                BasicImage basicImage = basicImageMapper.selectById(basicVideo.getCoverID());
                video.setImageFileName(basicImage.getFileName());
                feedInfo.setLearningFeedVideo(video);
            }


            //添加Topic

            LearningFeedTopic topic = learningFeedTopicMapper.selectOne(new QueryWrapper<LearningFeedTopic>().lambda().eq(LearningFeedTopic::getFeedId, fid.getId()));
            if (topic!=null){
                topic.setId(topic.getId());
                String topic1 = topicMapper.selectById(topic.getTopicId()).getName();
                topic.setTopicName(topic1);
                feedInfo.setLearningFeedTopic(topic);
            }

            //添加用户信息

            LearningFeed one = learningFeedMapper.selectOne(new QueryWrapper<LearningFeed>().lambda().eq(LearningFeed::getId, fid.getId()));
            User1 user = userMapper.selectOne(new QueryWrapper<User1>().lambda().eq(User1::getUserID, one.getAuthorId()));
            int age = getAgeByBirthday.getAgeByBirthday(user.getBirthday());
            user.setAge(age);
            feedInfo.setUser(user);

            //添加label

            LearningFeedLabel label = learningFeedLabelMapper.selectOne(new QueryWrapper<LearningFeedLabel>().lambda().eq(LearningFeedLabel::getFeedId, fid.getId()));
            if (label!=null){
            FeedLabel feedLabel = feedLabelMapper.selectOne(new QueryWrapper<FeedLabel>().lambda().eq(FeedLabel::getId, label.getLabelId()));
            label.setLabelName(feedLabel.getLabelName());
            feedInfo.setLearningFeedLabel(label);
            }

            //添加图片

            LearningFeedImage feedImage = learningFeedImageMapper.selectOne(new QueryWrapper<LearningFeedImage>().lambda().eq(LearningFeedImage::getFeedId, fid.getId()));
            if (feedImage!=null){
            BasicImage basicImage = basicImageMapper.selectById(feedImage.getImageId());
            feedImage.setFileName(basicImage.getFileName());
            feedInfo.setLearningFeedImage(feedImage);
            }
            //添加目标

            LearningFeedCategory category = learningFeedCategoryMapper.selectOne(new QueryWrapper<LearningFeedCategory>().lambda().eq(LearningFeedCategory::getFeedId, fid.getId()));
            if (category!=null){
            LearningCategory tail = learningCategoryMapper.selectById(category.getCategoryId());
            category.setName(tail.getName());
            feedInfo.setLearningFeedCategory(category);
            }
            feeds.add(feedInfo);
        });
        return feeds;
    }
*/
    public List<FeedInfo> listAllFeed1(){
        ArrayList<FeedInfo> feedInfo = new ArrayList<>();
        List<LearningFeed> feedsId = learningFeedMapper.selectList(new QueryWrapper<LearningFeed>().lambda().select(LearningFeed::getId));
        ArrayList<Integer> list = new ArrayList<>();
        feedsId.forEach(feed->{
            list.add(feed.getId());
        });
        //基本信息
        List<LearningFeed> feeds1 = learningFeedMapper.selectBatchIds(list);
        feeds1.forEach(fee->{
            FeedInfo feedInfo1 = new FeedInfo();
            LearningFeed feed = learningFeedMapper.selectById(fee.getId());
            feedInfo1.setId(feed.getId());
            feedInfo1.setAuthorID(feed.getAuthorId());
            feedInfo1.setLikeCount(feed.getLikeCount());
            feedInfo1.setPostTime(feed.getPostTime());
            feedInfo1.setReplyCount(feed.getReplyCount());
            feedInfo1.setText(feed.getType());

            feedInfo.add(feedInfo1);
        });

        //Video集合

        List<LearningFeedVideo> videos = learningFeedVideoMapper.selectBatchIds(list);
        if (!videos.isEmpty()){
            videos.forEach(video->{
                BasicVideo basicVideo = basicVideoMapper.selectById(video.getVideoID());
                video.setVideoFileName(basicVideo.getFileName());
                BasicImage basicImage = basicImageMapper.selectById(basicVideo.getCoverID());
                video.setImageFileName(basicImage.getFileName());
        });
        }

        //Topic集合

        List<LearningFeedTopic> topics = learningFeedTopicMapper.selectList(new QueryWrapper<LearningFeedTopic>().lambda().in(LearningFeedTopic::getFeedId, list));
        if (!topics.isEmpty()){
            topics.forEach(topic->{
                topic.setId(topic.getId());
                String topic1 = topicMapper.selectById(topic.getTopicId()).getName();
                topic.setTopicName(topic1);
            });
        }

        //用户信息集合

        List<LearningFeed> feeds = learningFeedMapper.selectList(new QueryWrapper<LearningFeed>().lambda().in(LearningFeed::getId, list));
        ArrayList<Integer> list1 = new ArrayList<>();
        feeds.forEach(feed->{
            list1.add(feed.getAuthorId());
        });
        List<User1> user1s = userMapper.selectList(new QueryWrapper<User1>().lambda().in(User1::getUserID, list1));
        user1s.forEach(user->{
        int age = getAgeByBirthday.getAgeByBirthday(user.getBirthday());
        user.setAge(age);
        });

        //label集合

        List<LearningFeedLabel> labels = learningFeedLabelMapper.selectList(new QueryWrapper<LearningFeedLabel>().lambda().in(LearningFeedLabel::getFeedId, list));
            if (!labels.isEmpty()){
        labels.forEach(label->{
                FeedLabel feedLabel = feedLabelMapper.selectOne(new QueryWrapper<FeedLabel>().lambda().eq(FeedLabel::getId, label.getLabelId()));
                label.setLabelName(feedLabel.getLabelName());
        });
            }

        //图片集合

        List<LearningFeedImage> images = learningFeedImageMapper.selectList(new QueryWrapper<LearningFeedImage>().lambda().in(LearningFeedImage::getFeedId, list));
        if (!images.isEmpty()){
             images.forEach(image->{
                BasicImage basicImage = basicImageMapper.selectById(image.getImageId());
                image.setFileName(basicImage.getFileName());
             });
        }

        //目标集合

        List<LearningFeedCategory> categories = learningFeedCategoryMapper.selectList(new QueryWrapper<LearningFeedCategory>().lambda().in(LearningFeedCategory::getFeedId, list));
        if (!categories.isEmpty()){
            categories.forEach(category->{
                LearningCategory tail = learningCategoryMapper.selectById(category.getCategoryId());
                category.setName(tail.getName());
            });
        }

        //装载集合

        feedInfo.forEach(feedInfo1 -> {
            videos.forEach(video->{
                if (video.getFeedID()==feedInfo1.getId()){
                    feedInfo1.setLearningFeedVideo(video);
                }
            });
            images.forEach(image->{
                if (image.getFeedId()==feedInfo1.getId()){
                    feedInfo1.setLearningFeedImage(image);
                }
            });
            topics.forEach(topic->{
                if (topic.getFeedId()==feedInfo1.getId()){
                    feedInfo1.setLearningFeedTopic(topic);
                }
            });
            labels.forEach(label->{
                if (label.getFeedId()==feedInfo1.getId()){
                    feedInfo1.setLearningFeedLabel(label);
                }
            });
            if (!categories.isEmpty()) {
                categories.forEach(category -> {
                    if (category.getFeedId() == feedInfo1.getId()) {
                        feedInfo1.setLearningFeedCategory(category);
                    }
                });
            }
        });
        return feedInfo;
    }



}
